package com.crazy.crazybusinessservice.service.Impl;

import com.crazy.crazybusinessservice.mapper.NetworthMapper;
import com.crazy.crazybusinessservice.service.DayInitializationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Date;

@Service
public class DayInitializationImpl implements DayInitializationService {

    @Autowired
    private NetworthMapper networthMapper;

    private Integer initializationStatus;

    // 获取当前日期
    private final LocalDate currentDate = LocalDate.now();

    @Override
    public boolean isWorkingDay() {

        // 获取当前日期的星期几
        DayOfWeek dayOfWeek = currentDate.getDayOfWeek();

        // 判断星期几是否是周一到周五
        return !(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY);
    }

    @Override
    public void updateAllNetworth() {
        networthMapper.updateDailyNetworth();
    }

    @Override
    public BigDecimal getNetWorthByProductID(int id) {
        return networthMapper.selectNetworthByproductId(id);
    }

    @Override
    public void insertDayInitialization() {

        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = dateFormat.format(currentDate);

        if (isWorkingDay()) {
            initializationStatus = 0;
            networthMapper.insertDayInitialization(initializationStatus,formattedDate);
        }
        else {
            initializationStatus = 1;
            networthMapper.insertDayInitialization(initializationStatus,formattedDate);
        }

    }

}
